import 'package:flustars/flustars.dart';
import 'package:fluttercontactpicker/fluttercontactpicker.dart';
import 'package:lishopflu/base/controller_import.dart';
import 'package:lishopflu/base/view_import.dart';
import 'package:lishopflu/entity/address_bean.dart';

class AddAddressCtl extends BaseController{

  var initProvince = ''.obs, initCity = ''.obs, initTown = ''.obs;
  var phoneCode = 86.obs;
  var nameCtl= TextEditingController().obs, phoneCtl = TextEditingController().obs, detailCtl = TextEditingController().obs;
  var isDefault = false.obs;
  var addressBean = AddressBean.init().obs;

  @override
  void onFirst() {
    if(appArguments!=null && appArguments.obj!=null){
      addressBean.value = appArguments.obj;
    }
    if(addressBean == null || addressBean.value == null){
      addressBean.value = AddressBean.init();
    }else{
      nameCtl.value.text = addressBean.value.name;
      initProvince.value = addressBean.value.province;
      initCity.value = addressBean.value.city;
      initTown.value = addressBean.value.district;
      detailCtl.value.text = addressBean.value.address;
      phoneCtl.value.text = addressBean.value.phone;
      phoneCode.value = addressBean.value.phoneCode;
      isDefault.value = StringUtil.text(addressBean.value.isDefault) == '1';
    }
  }

  @override
  void onDestroy() {
    nameCtl.value.dispose();
    phoneCtl.value.dispose();
    detailCtl.value.dispose();
  }

  void changeProvince(p, c, t) {
    initProvince.value = p;
    initCity.value = c;
    initTown.value = t;
    detailCtl.value.text = '';
  }

  void changeDefault(bool value){
    isDefault.value = value;
  }

  void changePhone(PhoneContact contact){
    nameCtl.value.text = contact.fullName;
    phoneCtl.value.text = StringUtil.trim(contact.phoneNumber.number);
  }

  void changeLoc(Map<String, Object> map){
    initProvince.value = map['province'].toString();
    initCity.value = map['city'].toString();
    initTown.value = map['district'].toString();
    detailCtl.value.text = map['address'].toString();
    addressBean.value.longitude = map['longitude'];
    addressBean.value.latitude = map['latitude'];
  }

  /*
    数据检查
   */
  String validBean(){
    String str= '';
    if(StringUtil.isEmpty(addressBean.value.name)){
      str = '请输入收货人！';
    }else if(StringUtil.isEmpty(addressBean.value.address)){
      str = '请输入手机号码！';
    }else if(StringUtil.isEmpty(addressBean.value.address)){
      str = '请选择省市区！';
    }else if(StringUtil.isEmpty(addressBean.value.address)){
      str = '请输入详细地址！';
    }
    return str;
  }

  /*
    保存
   */
  void save(OneCall<String> call){
    addressBean.value.name = nameCtl.value.text;
    addressBean.value.province = initProvince.value;
    addressBean.value.city = initCity.value;
    addressBean.value.district = initTown.value;
    addressBean.value.address = detailCtl.value.text;
    addressBean.value.phone = phoneCtl.value.text;
    addressBean.value.phoneCode = phoneCode.value;
    addressBean.value.isDefault = isDefault.value?'1':'0';
    String msg = validBean();
    call.call(msg);
  }
}